package org.csu.mall.controller;

import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.Category;
import org.csu.mall.service.ICategoryService;
import org.csu.mall.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/category/")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    //获取子类接口（一级） 参数中的categoryId指的是parentId
    @GetMapping("get_category")
    @ResponseBody
    public UniformResponse<List<Category>> getChildrenCategory(@RequestParam(name="categoryId",defaultValue = "0")Integer categoryId,
                                                               HttpSession session){
        UniformResponse<List<Category>> response=categoryService.getCategory(categoryId);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.CATEGORYLIST,response.getData());
        }
        return response;
    }

    //获取所有子类接口（本级及其所有子级） 参数中的categoryId指的是category表id
    @GetMapping("get_all_category")
    @ResponseBody
    public UniformResponse<List<Integer>> getAllChildrenCategory(@RequestParam(name="categoryId",defaultValue = "0")Integer categoryId,
                                                                 HttpSession session){
        UniformResponse<List<Integer>> response=categoryService.getAllChildrenCategory(categoryId);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.CATEGORYINDEXLIST,response.getData());
        }
        return response;
    }

    //添加品类接口
    @PostMapping("add_category")
    @ResponseBody
    public UniformResponse<Category> addCategory(@RequestParam(name="parentId",defaultValue = "0")Integer parentId,String categoryName,HttpSession session)
    {
        UniformResponse<Category> response=categoryService.addCategory(parentId,categoryName);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.ADDCATEGORY,response.getData());
        }
        return response;

    }

    //修改品类名称接口 参数中的ID指的是category表id
    @PostMapping("set_category_name")
    @ResponseBody
    public UniformResponse<Category> setCategoryName(Integer categoryId, String categoryName,HttpSession session){
        UniformResponse<Category> response=categoryService.setCategoryName(categoryId,categoryName);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.SETCATEGORYNAME,response.getData());
        }
        return response;
    }

    //获取子类的所有父类
    @PostMapping("get_parent_names")
    @ResponseBody
    public UniformResponse getParentName(Integer categoryId,HttpSession session)
    {
        UniformResponse response=categoryService.getParentName(categoryId);
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.PARENTNAMES,response.getData());
        }
        return response;

    }

    //获取所有分类
    @PostMapping("get_all_category_name")
    @ResponseBody
    public UniformResponse getAllCategoryName(HttpSession session)
    {
        UniformResponse response=categoryService.getAllCategory();
        if(response.isSuccess())
        {
            session.setAttribute(CONSTANT.ALLCATEGORY,response.getData());
        }
        return response;
    }

}
